﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="AddServiceCertificateOperation.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace CloudBlitz.DeploymentManager.DeploymentOperations
{
    using System;
    using System.Linq;

    using AzureManagement.AzureDataContracts;

    using CloudBlitz.DeploymentManager.DeploymentWorkflow;

    public class AddServiceCertificateOperation : DeploymentOperationBase
    {
        private readonly IAzureServiceClientsFactory azureServiceClientsFactory;

        public AddServiceCertificateOperation(IAzureServiceClientsFactory azureServiceClientsFactory) : base(1)
        {
            this.azureServiceClientsFactory = azureServiceClientsFactory;
        }

        protected override OperationResult ExecuteOverride(DeploymentContext context)
        {
            var azureServiceManagementClient =
                this.azureServiceClientsFactory.CreateServiceManagementClient(context.AzureCredentials);

            try
            {
                this.RaiseProgressChangedEvent(0, string.Format("Adding certificate with thumbprint {0} to hosted service \"{1}\"", context.ServiceCertificate.Thumbprint, context.HostedServiceName));

                var certificates = azureServiceManagementClient.ListServiceCertificates(context.HostedServiceName).Result;
                if (!certificates.Any(x => x.Thumbprint.Equals(context.ServiceCertificate.Thumbprint)))
                {
                    var operationId = azureServiceManagementClient.AddServiceCertificate(
                                                                                    context.HostedServiceName,
                                                                                    context.ServiceCertificate,
                                                                                    context.ServiceCertificatePassword).Result;

                    var operation = azureServiceManagementClient.WaitOperationForCompletion(operationId, TimeSpan.MaxValue);

                    if (operation.Status == OperationStatuses.Failed)
                    {
                        return new OperationResult(DeploymentOperationStatus.Failed, operation.Error.Message);
                    }
                }
            }
            catch (Exception e)
            {
                return ExceptionHelper.HandleException(e);
            }

            this.RaiseProgressChangedEvent(100, string.Format("Certificate with thumbprint {0} has been added to hosted service \"{1}\"", context.ServiceCertificate.Thumbprint, context.HostedServiceName));
            return OperationResult.Success;
        }
    }
}